<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>失物信息 - 校园失物招领系统</title>
    <link rel="stylesheet" href="/css/common.css">
</head>
<body>
    <div class="container">
        <nav class="navbar">
            <div class="nav-left"><h1>校园失物招领系统</h1></div>
            <div class="nav-right">
                <span id="username"></span>
                <a href="/notifications" class="nav-link">通知 <span id="unreadCount" class="badge"></span></a>
                <a href="/profile" class="nav-link">个人中心</a>
                <a href="javascript:void(0)" onclick="logout()" class="nav-link">退出</a>
            </div>
        </nav>

        <div class="main-content">
            <div class="sidebar">
                <a href="/home" class="menu-item">首页</a>
                <a href="/lost" class="menu-item active">失物信息</a>
                <a href="/found" class="menu-item">招领信息</a>
                <a href="/lost/publish" class="menu-item">发布失物</a>
                <a href="/found/publish" class="menu-item">发布招领</a>
                <a href="/my/lost" class="menu-item">我的失物</a>
                <a href="/my/found" class="menu-item">我的招领</a>
                <a href="/claims/sent" class="menu-item">发出的认领</a>
                <a href="/claims/received" class="menu-item">收到的认领</a>
                <a href="/favorites" class="menu-item">我的收藏</a>
                <div id="adminMenu"></div>
            </div>

            <div class="content">
                <h2>失物信息</h2>

                <div class="filters">
                    <div class="filter-row">
                        <div class="form-group" style="margin-bottom:0;">
                            <label>关键词</label>
                            <input type="text" id="keyword" placeholder="搜索标题或描述">
                        </div>
                        <div class="form-group" style="margin-bottom:0;">
                            <label>分类</label>
                            <select id="categoryId">
                                <option value="">全部</option>
                            </select>
                        </div>
                        <div class="form-group" style="margin-bottom:0;">
                            <label>状态</label>
                            <select id="status">
                                <option value="">全部</option>
                                <option value="0">寻找中</option>
                                <option value="1">已找到</option>
                            </select>
                        </div>
                        <div class="form-group" style="margin-bottom:0;">
                            <label>&nbsp;</label>
                            <button class="btn" onclick="search()">搜索</button>
                        </div>
                    </div>
                </div>

                <div class="section">
                    <div id="itemList"></div>
                    <div id="pagination" class="pagination"></div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="/js/common.js"></script>
    <script>
        loadUser();
        loadUnreadCount();

        let currentPage = 1;
        const pageSize = 10;

        $.get('/api/category/list', function(res) {
            if (res.code === 200) {
                res.data.forEach(cat => {
                    $('#categoryId').append(`<option value="${cat.id}">${cat.name}</option>`);
                });
            }
        });

        function search() {
            currentPage = 1;
            loadData();
        }

        function loadData() {
            const params = {
                keyword: $('#keyword').val(),
                categoryId: $('#categoryId').val(),
                status: $('#status').val(),
                page: currentPage,
                pageSize: pageSize
            };

            $.get('/api/lost/list', params, function(res) {
                if (res.code === 200) {
                    const items = res.data.items;
                    const total = res.data.total;

                    if (items.length === 0) {
                        $('#itemList').html('<p class="empty">暂无失物信息</p>');
                    } else {
                        const html = items.map(item => `
                            <div class="item-card">
                                <h4><a href="/lost/detail?id=${item.id}">${item.title}</a></h4>
                                <p>${item.description || ''}</p>
                                <div class="item-meta">
                                    <span>分类：${item.categoryName}</span>
                                    <span>丢失地点：${item.lostLocation}</span>
                                    <span>丢失时间：${formatDate(item.lostTime)}</span>
                                    <span>状态：${item.status === 0 ? '寻找中' : '已找到'}</span>
                                    <span>浏览：${item.views}次</span>
                                </div>
                            </div>
                        `).join('');
                        $('#itemList').html(html);
                    }

                    renderPagination(total);
                }
            });
        }

        function renderPagination(total) {
            const totalPages = Math.ceil(total / pageSize);
            if (totalPages <= 1) {
                $('#pagination').html('');
                return;
            }

            let html = `<button ${currentPage === 1 ? 'disabled' : ''} onclick="changePage(${currentPage - 1})">上一页</button>`;
            for (let i = 1; i <= totalPages; i++) {
                html += `<button class="${i === currentPage ? 'active' : ''}" onclick="changePage(${i})">${i}</button>`;
            }
            html += `<button ${currentPage === totalPages ? 'disabled' : ''} onclick="changePage(${currentPage + 1})">下一页</button>`;
            $('#pagination').html(html);
        }

        function changePage(page) {
            currentPage = page;
            loadData();
        }

        loadData();
    </script>
</body>
</html>

